package cn.iocoder.yudao.module.dr.controller.admin.collection3dacquisition;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.dr.controller.admin.collection3dacquisition.vo.Collection3dAcquisitionPageReqVO;
import cn.iocoder.yudao.module.dr.controller.admin.collection3dacquisition.vo.Collection3dAcquisitionRespVO;
import cn.iocoder.yudao.module.dr.controller.admin.collection3dacquisition.vo.Collection3dAcquisitionSaveReqVO;
import cn.iocoder.yudao.module.dr.dal.dataobject.collection3dacquisition.Collection3dAcquisitionDO;
import cn.iocoder.yudao.module.dr.dal.dataobject.collectionlinkaddress.CollectionLinkAddressDO;
import cn.iocoder.yudao.module.dr.service.collection3dacquisition.Collection3dAcquisitionService;
import cn.iocoder.yudao.module.dr.service.collectionlinkaddress.CollectionLinkAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 藏品三维采集信息")
@RestController
@RequestMapping("/dr/collection3d-acquisition")
@Validated
public class Collection3dAcquisitionController {

    @Resource
    private Collection3dAcquisitionService collection3dAcquisitionService;

    @Resource
    private CollectionLinkAddressService collectionLinkAddressService;

    @PostMapping("/create")
    @Operation(summary = "创建藏品三维采集信息")
    @PreAuthorize("@ss.hasPermission('dr:collection3d-acquisition:create')")
    public CommonResult<Long> createCollection3dAcquisition(@Valid @RequestBody Collection3dAcquisitionSaveReqVO createReqVO) {
        return success(collection3dAcquisitionService.createCollection3dAcquisition(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新藏品三维采集信息")
    @PreAuthorize("@ss.hasPermission('dr:collection3d-acquisition:update')")
    public CommonResult<Boolean> updateCollection3dAcquisition(@Valid @RequestBody Collection3dAcquisitionSaveReqVO updateReqVO) {
        collection3dAcquisitionService.updateCollection3dAcquisition(updateReqVO);
        return success(true);
    }


    @PutMapping("/audit")
    @Operation(summary = "审核藏品三维采集信息")
    @PreAuthorize("@ss.hasPermission('dr:collection3d-acquisition:update')")
    public CommonResult<Boolean> auditCollection3dAcquisition(@Valid @RequestBody Collection3dAcquisitionSaveReqVO updateReqVO) {
        collection3dAcquisitionService.auditCollection3dAcquisition(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除藏品三维采集信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dr:collection3d-acquisition:delete')")
    public CommonResult<Boolean> deleteCollection3dAcquisition(@RequestParam("id") Long id) {
        collection3dAcquisitionService.deleteCollection3dAcquisition(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得藏品三维采集信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dr:collection3d-acquisition:query')")
    public CommonResult<Collection3dAcquisitionRespVO> getCollection3dAcquisition(@RequestParam("id") Long id) {
        Collection3dAcquisitionDO collection3dAcquisition = collection3dAcquisitionService.getCollection3dAcquisition(id);
        return success(BeanUtils.toBean(collection3dAcquisition, Collection3dAcquisitionRespVO.class));
    }


    @GetMapping("/page")
    @Operation(summary = "获得藏品三维采集信息分页")
    @PreAuthorize("@ss.hasPermission('dr:collection3d-acquisition:query')")
    public CommonResult<PageResult<Collection3dAcquisitionRespVO>> getCollection3dAcquisitionPage(@Valid Collection3dAcquisitionPageReqVO pageReqVO) {
        PageResult<Collection3dAcquisitionDO> pageResult = collection3dAcquisitionService.getCollection3dAcquisitionPage(pageReqVO);

        pageResult.getList().forEach(o -> {
            List<CollectionLinkAddressDO> covers = collectionLinkAddressService.getCover(o.getId(),"3","0");
            if(CollectionUtil.isNotEmpty(covers)){
                o.setCollectionCover(covers.get(0).getLinkAddress());
            }
        });

        pageResult.getList().forEach(o -> {
            List<CollectionLinkAddressDO> covers = collectionLinkAddressService.getCover(o.getId(),"3","5");
            if(CollectionUtil.isNotEmpty(covers)){
                o.setLinkAddress(covers.get(0).getLinkAddress());
            }
        });
        return success(BeanUtils.toBean(pageResult, Collection3dAcquisitionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出藏品三维采集信息 Excel")
    @PreAuthorize("@ss.hasPermission('dr:collection3d-acquisition:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCollection3dAcquisitionExcel(@Valid Collection3dAcquisitionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<Collection3dAcquisitionDO> list = collection3dAcquisitionService.getCollection3dAcquisitionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "藏品三维采集信息.xls", "数据", Collection3dAcquisitionRespVO.class,
                        BeanUtils.toBean(list, Collection3dAcquisitionRespVO.class));
    }

}